package cn.cd.anStudio;

import java.util.Calendar;
import java.util.Vector;

import cn.cd.anStudio.util.MyBusCommonUtil;

import android.text.format.Time;
import android.util.Log;

public class MyBusInfo {
	
	private static final String TAG = "MyBusInfo";
	
	public static final int BUS_UNKNOWN_DIRECTION = -1;
	public static final int BUS_BI_DIRECTION = 0;
	public static final int BUS_CIRCLE = 1;
	public static final int BUS_SINGLE_DIRECTION = 2;
	
	private String busNum;
	private int routeType;
	private Time busStart = null;
	private Time busEnd   = null;
	private Vector<String> busstopList1 = new Vector<String>();
	private Vector<String> busstopList2 = new Vector<String>();
	
	public MyBusInfo() {
		super();
	}
	
	public MyBusInfo(String busNum, int routeType) {
		super();
		this.busNum = busNum;
		this.routeType = routeType;
	}
	
	public MyBusInfo(String busNum) {
		this.busNum = busNum;
	}
	
	public void setBusNum(String busNum) {
		this.busNum = busNum;
	}

	public void setRouteType(int routeType) {
		this.routeType = routeType;
	}
	
	public void setStartTime(Time startTime) {
		this.busStart = startTime;
	}
	
	public void setEndTime(Time endTime) {
		this.busEnd = endTime;
	}
	
	public void addBusstopList1(Vector<String> list) {

		for (String tmp : list) {
			busstopList1.add(MyBusCommonUtil.formatPlainString(tmp, null).replaceAll(" ", ""));
		}
	}
	
	public void addBusstopList2(Vector<String> list)
	    throws IllegalArgumentException {
		if (BUS_BI_DIRECTION == routeType) {
			
			for (String tmp : list) {
				busstopList2.add(MyBusCommonUtil.formatPlainString(tmp, null).replaceAll(" ", ""));
			}
			
			return;
		}
		
		throw new IllegalArgumentException();
	}
	
	public String getBusNum() {
		return this.busNum;
	}
	
	public int getRouteType() {
		return this.routeType;
	}
	
	public Vector<String> getBusstopList1() {
		return this.busstopList1;
	}
	
	public Vector<String> getBusstopList2()
			throws IllegalArgumentException {
		if (BUS_BI_DIRECTION == routeType) {
			return this.busstopList2;
		}
		
		throw new IllegalArgumentException();
	}
	
	public String getBusStart1() {
		
		if (busstopList1.size() > 0)
			return this.busstopList1.firstElement();
		
		return null;
	}
	
    public String getBusTerm1() {
		
    	if (busstopList1.size() > 0)
    		return this.busstopList1.lastElement();
    	
    	return null;
	}
    
    public String getBusStart2()
    		throws IllegalArgumentException {
    	
    	if (BUS_BI_DIRECTION == routeType) {
    		if (busstopList2.size() > 0)
    			return this.busstopList2.firstElement();
    		
    		return null;
		}
		
		throw new IllegalArgumentException();
	}
	
    public String getBusTerm2()
    		throws IllegalArgumentException {
    	
    	if (BUS_BI_DIRECTION == routeType) {
    		if (busstopList2.size() > 0)
    			return this.busstopList2.lastElement();
    		
    		return null;
		}
		
		throw new IllegalArgumentException();
	}
    
    public Time getStartTime() {
		return this.busStart;
	}
	
	public Time getEndTime() {
		return this.busEnd;
	}
	
	public boolean isBusValidThisTime() {
		
		final Calendar mCalendar=Calendar.getInstance();
        int mHour=mCalendar.get(Calendar.HOUR_OF_DAY);
        int mMinuts=mCalendar.get(Calendar.MINUTE);
        
        Time thisTime = new Time();
        thisTime.hour = mHour;
        thisTime.minute = mMinuts;
		
        if (this.busEnd.after(this.busStart)) {
        	//For bus at day time
			if (thisTime.after(this.busStart)
					&&thisTime.before(this.busEnd)) {
				Log.v(TAG, "Is Valid Time");
				return true;
			}
        } else {
        	//For bus at night time
        	if (thisTime.after(this.busStart)
					||thisTime.before(this.busEnd)) {
				Log.v(TAG, "Is Valid Time");
				return true;
			}
        }

		Log.v(TAG, "Time invalid");
		return false;
	}
	
	public void traceInfo() {
		Log.v(TAG, "================Bus Information================");
		Log.v(TAG, "== Bus Numer : " + busNum);
		if (busStart != null)
			Log.v(TAG, "== Bus Start : " + busStart.hour + ":" + busStart.minute);
		if (busEnd != null)
			Log.v(TAG, "== Bus End : " + busEnd.hour + ":" + busEnd.minute);
		Log.v(TAG, "== Bus Stops List 1 :");
		for (int i = 0; i < busstopList1.size(); i++) {
			Log.v(TAG, "==       " + busstopList1.elementAt(i));
		}

		if (BUS_BI_DIRECTION == routeType) {
			Log.v(TAG, "== Bus Stops List 2 :");
			for (int i = 0; i < busstopList2.size(); i++) {
				Log.v(TAG, "==       " + busstopList2.elementAt(i));
			}
		}
		
		Log.v(TAG, "======================END======================");
	}
}
